package cn.javabb.common.utils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2019/1/11/011 17:46
 * @Since V1.0
 */
@Component
public final class ThUtil {

	/**
	 * 截取字符串
	 *
	 * @param str
	 * @param len
	 * @return
	 */
	public static String substr(String str, int len) {
		if (str.length() > len) {
			return str.substring(0, len);
		}
		return str;
	}

	/**
	 * 封装栏目url
	 * 
	 * @param id
	 * @return
	 */
	public static String catalog_url(Integer id) {
		return "/post/" + id+"/1";
	}

	public static String catalog_img(String img) {

		return "/assets/images/portal_user_img.png";
	}

	public static String formatUserImage(String img) {
		return StrUtil.isBlank(img) ? "/assets/images/default_userImg.png" : img;
	}
	
	public static String formatPublishDate(String time) {
		return DateUtil.getShortTime(time);
	}

	public static boolean isNewest(String time){
		if (StrUtil.isBlank(time)) {
			return false;
		}
		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String timeFormat = sdf.format(date);
		//String time1 = cn.hutool.core.date.DateUtil.parse(cn.hutool.core.date.DateUtil.date(Calendar.getInstance().getTimeInMillis()),"yyyy-MM-dd hh:mm:ss");
		//String time1 = cn.hutool.core.date.DateUtil.format(cn.hutool.core.date.DateUtil.parse(time,"yyyy-MM-dd hh:mm:ss"),"yyyy-MM-dd hh:mm:ss");
		String time2 = cn.hutool.core.date.DateUtil.format(new Date(), "yyyy-MM-dd hh:mm:ss");
		long a = cn.hutool.core.date.DateUtil.between(cn.hutool.core.date.DateUtil.parseDate(timeFormat),cn.hutool.core.date.DateUtil.parseDate(time2), DateUnit.HOUR);
		if(a<=24){
			return true;
		}
		return false;
	}

	public static List<String> toList(String string) {
		return ListUtils.stringToListString(string);
	}
	public static void main(String[] args) {
		isNewest(null);
	}
}
